<?php
/**
 * Created by : PhpStorm
 * User: ZPF
 * Date: 2022/4/19
 * Time: 22:36
 */

namespace app\admin\model;

use think\Model;
use think\model\concern\SoftDelete;

class ClassifyModel extends BaseModel
{
//    use SoftDelete;
//    protected $deleteTime = 'delete_time';
    protected $name='classify';
    protected $jsonAssoc=true;
    protected $json=['attribute_name','attribute_spec','sku_mold_id','stock_attribute'];
    public function skuMold(){
        return $this->belongsTo(SkuMoldModel::class,'mold_id','id');
    }
    public function creatUser(){
        return $this->belongsTo(AdminModel::class,'creator');
    }
    public static function onAfterInsert($model){

        //判断当前sku是否复制而来，复制的sku不需要生成编码，否则需要
        if(empty($model->code)) {
//            $skuId=$model->sku_mold_id[count($model->sku_mold_id)-1];
            $skuId=$model->mold_id;
            $moldInfo=SkuMoldModel::find($skuId)->toArray();
//            $model->code = $model->toArray()['type'] . $moldInfo['code'] . substr((string)(100000 + $moldInfo['flow_code']+1), 1);
            $code = preg_replace('/[^a-zA-Z]/', '', $moldInfo['code'] );
            $model->code = $code . substr((string)(100000 + $moldInfo['flow_code']+1), 1);
            SkuMoldModel::where('id',$skuId)->inc('flow_code')->update();
            $model->save();
        }

    }
    public static function onAfterDelete($model)
    {
        @unlink(app()->getRootPath() . 'public/static/upload/'.$model->file_path);
        @unlink(app()->getRootPath() . 'public/static/upload/'.$model->pic_path);
    }

}
